<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')" :align-center="true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="130px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="不良品项编码" prop="rejects_code">
                        <el-input v-model="form.rejects_code" placeholder="请输入不良品项编码" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="不良品项名称" prop="rejects_name">
                        <el-input v-model="form.rejects_name" placeholder="请输入不良品项名称" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="24">
                <el-form-item label="不良品项名称" prop="rejects_name">
                    <el-button type="primary" size="small" @click="selectItem">选择检验项</el-button>
                    <sc-form-table ref="table" v-model="detail" :addTemplate="addTemplate" placeholder="暂无数据" height="250" hideAdd>
                        <el-table-column label="编号" prop="code" align="center"></el-table-column>
                        <el-table-column label="名称" prop="name" align="center"></el-table-column>
                        <el-table-column label="检验标准" prop="standard" align="center"></el-table-column>
                        <el-table-column label="方法" prop="method" align="center"></el-table-column>
                        <el-table-column label="检验单位" prop="unit" align="center"></el-table-column>
                        <el-table-column label="标准值" prop="stvalue" align="center"></el-table-column>
                        <el-table-column label="检验上限" prop="upper" align="center"></el-table-column>
                        <el-table-column label="检验下限" prop="lower" align="center"></el-table-column>
                    </sc-form-table>
                </el-form-item>
            </el-col>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <item-list v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></item-list>
</template>

<script>
import ItemList from '../inspectitem/itemList.vue';

export default {
	emits: ['success', 'closed'],
	components: {
        ItemList,
	},
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
				edit:'编辑',
				show:'查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				rejects_code:'',
				rejects_name:'',
			},
            detail: [],
			//验证规则
			rules: {
				end_price: [
					{ required: true, message: '请输入尾款' }
				],
			},
            dialog: {
				save: false,
			},
            addTemplate: {},
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
            let array = []
            this.detail.map(item=>{
                array.push(item.id)
            })
            this.form.check_project = array
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
                    if (this.form.id) {
						res = await this.$API.craft.rejects.edit.post(this.form);
					} else {
						res = await this.$API.craft.rejects.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
			this.form.id = data.id
            var res = await this.$API.craft.rejects.detail.post({id:data.id})
			this.form.rejects_code = res.data.rejects_code
			this.form.rejects_name = res.data.rejects_name
            this.detail = res.data.detail
		},
        selectItem(){
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('add')
            })
        },
        handleSuccess(data){
            let array = []
            data.map((item)=>{
                array.push({
                    id:item.id,
                    name:item.name,
                    code:item.code,
                    standard:item.standard,
                    method:item.method,
                    unit:item.unit,
                    stvalue:item.stvalue,
                    upper:item.upper,
                    lower:item.lower,
                })
            })
            let existingIds = this.detail.map(item => item.id);
            for(let i = array.length - 1; i >= 0; i--){
                if(existingIds.includes(array[i].id)){
                    array.splice(i, 1);
                }
            }
            this.detail = [...this.detail,...array]
        },
	}
}
</script>

<style lang="scss">
</style>
